// pages/type_lists/type_lists.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */

    // 由于该接口导出有些文章没有阅读模式，需要提前拿到数据判断是否渲染这些文章列表

    data: {
        link: "",
        lists: [],
        first_id: "",
        skin_style:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        app.getSkin();
        that.setData({
            link: "https://news-at.zhihu.com/api/4/theme/" + options.id,
            skin_style:app.globalData.skin
        })
        wx.request({
            url: that.data.link,
            success: function (res) {
                that.setData({
                    lists: res.data,
                    first_id: res.data.stories[0].id
                })
            }
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        // 显示顶部刷新图标  
        wx.showNavigationBarLoading();
        var that = this;
        wx.request({
            url: that.data.link,
            success: function (res) {
                that.setData({
                    lists: res.data
                })
                // 隐藏导航栏加载框  
                wx.hideNavigationBarLoading();
                // 停止下拉动作  
                wx.stopPullDownRefresh();
            }
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        // 显示加载图标  
        wx.showLoading({
            title: '玩命加载中',
        })
        wx.request({
            url: that.data.link + "/before/" + that.data.first_id,
            success: function (res) {
                that.data.lists.stories = that.data.lists.stories.concat(res.data.stories);
                // 设置数据  
                that.setData({
                    lists: that.data.lists
                })
                // 隐藏加载框  
                wx.hideLoading();
            }
        })
    },
    to_content: function (e) {
        let content_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../content/content?id=' + content_id,
        });
    }
})